/**
 * <strong>Title : EquipmentDownTimeController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月19日 上午11:11:43<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.condition.EquipmentDownTimeCondition;
import cn.b.sky.mes.em.service.EquipmentDownTimeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.Equipment;
import cn.b.sky.mes.em.model.EquipmentDownTime;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : EquipmentDownTimeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备停机时间控制器<br> 
 * <strong>Create on : 2015年11月19日 上午11:11:43<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/equipmentDownTime/*")
public class EquipmentDownTimeController extends SecurityController {
  @Resource
  EquipmentDownTimeService equipmentDownTimeService;

  @RequestMapping("equipmentDownTime_list")
  public String selectEquipmentDownTimeListByCondition(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentDownTimeCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<EquipmentDownTime> list = equipmentDownTimeService.selectEquipmentDownTimeListByCondition(condition);
    int count = equipmentDownTimeService.selectEquipmentDownTimeCountByCondition(condition);

    condition.setData(list);
    condition.setTotalCount(count);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "em/equipmentDownTime/equipmentDownTime_list";

  }

  @RequestMapping("addEquipmentDownTime")
  public String addEquipmentDownTime(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "em/equipmentDownTime/equipmentDownTime_add";
  }

  @RequestMapping("modifyEquipmentDownTime")
  public String modifyEquipmentDownTime(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentDownTime time = equipmentDownTimeService.getEquipmentDownTime(Long.valueOf(id));
    model.addAttribute("equipmentDownTime", time);
    model.addAttribute("pattern", "modify");
    return "em/equipmentDownTime/equipmentDownTime_add";
  }

  @RequestMapping("viewEquipmentDownTime")
  public String viewEquipmentDownTime(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentDownTime time = equipmentDownTimeService.getEquipmentDownTime(Long.valueOf(id));
    model.addAttribute("equipmentDownTime", time);
    model.addAttribute("pattern", "view");
    return "em/equipmentDownTime/equipmentDownTime_view";
  }

  @RequestMapping("saveEquipmentDownTime")
  @ResponseBody
  public DWZResult saveEquipmentDownTime(Model model, String navTabId, String id, EquipmentDownTime time) {

    if (time == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    setCreateInfo(time);
    time.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(id)) {
      equipmentDownTimeService.insertEquipmentDownTime(time);
      String forwardUrl = "em/equipmentDownTime/equipmentDownTime_list";
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, forwardUrl, null);
    } else {

      time.setId(Long.valueOf(id));
      equipmentDownTimeService.updateEquipmentDownTime(time);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    }
  }

  @RequestMapping(value = "deleteEquipmentDownTime")
  @ResponseBody
  public DWZResult deleteEquipmentDownTime(Model model, String navTabId, Equipment equipment, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] idArry = ids.split(",");
    int len = idArry.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    int failureCount = 0;
    for (int i = 0; i < idArry.length; i++) {
      reports[i] = equipmentDownTimeService.deleteEquipmentDownTime(Long.valueOf(idArry[i]));
      if (!reports[i].isSuccess()) {
        failureCount += 1;
      }
    }
    if (failureCount == 0) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

}
